// user模型
package model

import (
	"gin_test/core"
	"gin_test/utils"
)

type UserModel struct {
	ID        uint        `gorm:"column:id;comment:'主键';primaryKey;not null" json:"id"`
	UserName  string      `gorm:"column:username;varchar(50);comment:'用户名'" json:"username"`
	Email     string      `gorm:"column:email;varchar(100);comment:'用户名';unique'" json:"email"`
	CreatedAt utils.HTime `gorm:"column:created_at;type:timestamp;comment:'创建时间'" json:"created_at"`
}

func (UserModel) TableName() string {
	return "users"
}

type AddUser struct {
	UserName string `json:"username"`
	Email    string `json:"email"`
}

// GetUserByEmail 获取用户信息
func GetUserByEmail(email string) (user UserModel) {
	core.DB.Where("email = ?", email).First(&user)
	return user
}

// CreateUser 创建用户
func CreateUser(user *UserModel) error {
	res := core.DB.Create(&user)
	return res.Error
}
